/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Info Cram 6000.
 *
 * The Initial Developer of the Original Code is
 * Jiří Mašek.
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */

/* ************************************************************************** *
 * ******************************** EXTRACTOR ******************************* *
 * ************************************************************************** */

/**
 * Spustí extrakci podle aktuálně editovaného konfiguračního souboru.
 */
infocram.extractor.exec = function() {
    var _tree = this._parent.tree;

    // získání objektu s konfigurací extraktoru
    var def = _tree.getExtbrainExtractDefinition();
    
    if (!def)
        return;

    var log = document.getElementById("log");

    var ret = document.getElementById("ret");

    // stuštění extraktoru
    extract(def, function(message) {
        log.value += message;
    }, function(ret) {
        try {
            ret.value = JSON.stringify(ret);
        } catch(e) {
            ret.value = e;
        }
    });
}

/* ************************************************************************** *
 * ************************* EXTRACTOR EVENT HANDLER ************************ *
 * ************************************************************************** */

/**
 * Handler zajišťující spuštění extraktoru při načtení stránky obsahující objekt
 * <code>extbrain_extract_definition</code>.
 */
var extractorEventHandler = {
    init: function() {
        var currentBrowser  = aardvarkUtils.currentBrowser();

        currentBrowser.addEventListener("DOMContentLoaded", this.onPageLoad, true);
    },

    onPageLoad: function(aEvent) {
        var doc = aEvent.originalTarget;
        if (!doc)
            return;
        this.extbrain_extract_definition = null;
        var objRegExp = /extbrain_extract_definition[ ]?=[ ]?\{[\n a-zA-z:'"0-9,\{\}\.#\[\]_\-\/\(\)]*\};/g;
        var matches = objRegExp.exec((new XMLSerializer()).serializeToString(doc));
        var extbrain_extract_definition;
        if (matches != null) {
            eval(matches[0]);
        }
        var def = extbrain_extract_definition;
        if (!def)
            return;
        var document = doc;
        var body = document.getElementsByTagName("body")[0];
        var log = document.createElement("pre");
        log.setAttribute("class", "log");
        body.appendChild(log);
        extract(def, function(message) {
            log.textContent += message;
        }, function(ret) {
            var output = document.createElement("div");
            output.setAttribute("class", "output");
            jQuery.render(output, ret);
            body.appendChild(output);
            var json = document.createElement("textarea");
            json.setAttribute("class", "json");
            try {
                json.value = JSON.stringify(ret);
            } catch(e) {
                json.value = e;
            }
            body.appendChild(json);
        });
    }
}

window.addEventListener("load", function() {
    extractorEventHandler.init();
}, false);